#!/usr/bin/perl -w # for syntax checking
# CSS-V1

use gri_secmgr qw(get_cookie_value got_cookie);
use gri_database;
use gri_mime;
use Data::Dumper;

sub mm_docrepos_show_section_docs {
my $section=shift;
my $page=shift;
my $order=shift;

	my %ftypes=();
	my $mime_info=undef;
	my $mime_file=$::GRI_FRONTEND->get_variable("MIME_FILE");
	if(defined($mime_file)) {
		$mime_info=new gri_mime($mime_file);
	}
		
	if(! got_cookie(name => "gri_dbm_intranet")) {
		print STDERR "mm_send_email: User not logged in - no email sent!\n";
		return "Unable to send mail - user not logged in.";
	}
	my $v=get_cookie_value(name=>"gri_dbm_intranet");
	$v=~ s/\s+.*//;
	my $username=$v;

	# Work out whether use can access section and abort with
	# suitable error if not.

	my $db_dir=$::GRI_FRONTEND->get_global_directory("database");
	my $docrepos_dir=$::GRI_FRONTEND->get_global_directory("docrepos");
	my $DB=new gri_database("$db_dir/gri.db");
	$section="" if $section eq "?section - not defined?";
	$page=1 if $section eq "?page - not defined?";
	$order="name" if $section eq "?order - not defined?";

	if(! grep {$order eq $_} ("name","rname","upload","rupload","type","rtype","size","rsize")) {
		$order="name";
	}

	$section=decode_base64($section);
	my ($a,$b)=$DB->get_section(section=>$section);
	if($a!=1) {
		return "Error: Section specified is unknown.";
	}

	# Work out whether the user has access to this section and 	#
	# if not we abort before showing anything.

	my ($i,$i2)=$DB->get_user_sections(user=>$username,type=>"reader");
	my ($j,$j2)=$DB->get_user_sections(user=>$username,type=>"publisher");
	if($i!=1) {
		return "Error: Unable to determine whether user has access or not.";
	}
	if(! grep {$section eq $_} @$i2) {
		return "Error: User does not have reader access to this section.";
	}
	my $d;
	if(exists($::ENV{PATH_INFO}) && length($::ENV{PATH_INFO})>1) {
		$d="../images";
	} else {
		$d="images";
	}

	# We get all the details of all the files that are present in
	# this section. Each page consists of 20 documents...

	my ($rc,$docs,$tdocs)=$DB->get_docs(section=>$section,order=>$order,page=>$page,pagesize=>20);

	# If rc==1 then we have something in docs to show. tdocs is the total number
	# of documents available...
	#
	# All the documents will be shown in a table, where the headers allow
	# the sort order to be changed if necessary. Changint the sort order will
	# always move back to page 1 though...

	# Number, Name, Uploaded, Size, Type,
	# Description, Actions

	# The Actions will have several links - view, [delete], [replace]
	# The delete and replace will only be possible if the user
	# has publisher writes to this section. The delete will remove it
	# from this section only, as will replace - hence it might lead to some
	# duplication...
	# show each file name, desciption, size, ...

	my $back_link=1;
	my $forward_link=1;
	my $first_doc=($page-1)*20+1;
	if($first_doc==1) {
		$back_link=0;
	}
	my $last_doc=$page*20;
	if($tdocs<=$last_doc) {
		$forward_link=0;
		$last_doc=$tdocs;
	}
	if($last_doc<1) {
		$s="Page $page [No published documents as yet]<br><br>\n";
	} else {
		$s="<font class=\"stdtext\">Page $page [Documents $first_doc - $last_doc]<br><br></font>\n";
	}
	$s.="<table class=gri_container-minsize width=100% cols=5>\n";
	$s.="<tr class=gri_headerrow>\n";
	$s.="<td class=gri_cell width=5%>Number</td>";

	# "name","rname","upload","rupload","type","rtype","size","rsize")

	# Do the Name column header
	$s.="<td class=gri_cell width=50%>";
	if($order eq "name") {
		my $l=link_to_str("headinglink","Name","docrepos_show_section_docs","section=" . _encode_base64($section) . ",page=1,order=rname");
		$s.=$l . "&nbsp;&nbsp;<img src=\"$d/down_16.png\"></td>";
	} else {
		my $l=link_to_str("headinglink","Name","docrepos_show_section_docs","section=" . _encode_base64($section) . ",page=1,order=name");
		$s.=$l . "&nbsp;&nbsp;<img src=\"$d/up_16.png\"></td>";
	}

	# Do the Uploaded column header
	$s.="<td class=gri_cell width=15%>";
	if($order eq "name") {
		my $l=link_to_str("headinglink","Uploaded","docrepos_show_section_docs","section=" . _encode_base64($section) . ",page=1,order=ruploaded");
		$s.=$l . "&nbsp;&nbsp;<img src=\"$d/down_16.png\"></td>";
	} else {
		my $l=link_to_str("headinglink","Uploaded","docrepos_show_section_docs","section=" . _encode_base64($section) . ",page=1,order=uploaded");
		$s.=$l . "&nbsp;&nbsp;<img src=\"$d/up_16.png\"></td>";
	}

	# Do the Size column header
	$s.="<td class=gri_cell width=15%>";
	if($order eq "size") {
		my $l=link_to_str("headinglink","Size","docrepos_show_section_docs","section=" . _encode_base64($section) . ",page=1,order=rsize");
		$s.=$l . "&nbsp;&nbsp;<img src=\"$d/down_16.png\"></td>";
	} else {
		my $l=link_to_str("headinglink","Size","docrepos_show_section_docs","section=" . _encode_base64($section) . ",page=1,order=size");
		$s.=$l . "&nbsp;&nbsp;<img src=\"$d/up_16.png\"></td>";
	}

	# Do the Type column header
	$s.="<td class=gri_cell width=15%>";
	if($order eq "type") {
		my $l=link_to_str("headinglink","Type","docrepos_show_section_docs","section=" . _encode_base64($section) . ",page=1,order=rtype");
		$s.=$l . "&nbsp;&nbsp;<img src=\"$d/down_16.png\"></td>";
	} else {
		my $l=link_to_str("headinglink","Type","docrepos_show_section_docs","section=" . _encode_base64($section) . ",page=1,order=type");
		$s.=$l . "&nbsp;&nbsp;<img src=\"$d/up_16.png\"></td>";
	}
	$s.="</tr>\n";

	my $row=0;
	my $cdoc_num=$first_doc;
	my $allow_delete=0;
	#################################################################
	# If the user is in the publisher list for this section then	#
	# we allow them to delete the file...				#
	#################################################################
	if(grep {$section eq $_} @$j2) {
		$allow_delete=1;
	}
	my $url_prefix=$::GRI_FRONTEND->get_url_prefix();
	for my $cr (@$docs) {
		#########################################################
		# Check the version_doc_index to look for historical	#
		# entries of each document, and if found we add a 	#
		# history link for this document.			#
		#########################################################
		my $history_link="";
		my $hh=$DB->has_history(lname => $cr->{local_name});
		print STDERR "has_history(lname=>$cr->{local_name})=$hh\n";
		if($hh>0) {
			$history_link=link_to_str("stdlink","History","docrepos_view_document_history","section=" . _encode_base64($section) . ",document_sha1=$cr->{content_sha1},lname=$cr->{local_name}");
			
		}
		my $_name=$cr->{original_name};
		$_name =~ s/.*\\//;
		$_name =~ s/.*\///;
		my $uploaded=$cr->{uploaded_time};
		my $size=0;
		my @SS=stat("$docrepos_dir/$cr->{local_name}");
		if(@SS) {
			$size=$SS[7];
		}
		$size=sprintf("%9.1f Kb",$size/1024);
		$size =~ s/ /&nbsp;/g;
		
		my @lt=localtime($uploaded);
		$uploaded=sprintf("%02d/%02d/%04d %02d:%02d",$lt[3],$lt[4]+1,$lt[5]+1900,$lt[2],$lt[1]);
		my $type="unknown";
		if($mime_info) {
			$type=$mime_info->description($_name);
		}
		if(!defined($type)) {
			$type="?unknown?";
			print STDERR "Error: Unable to determine the type of '$_name'.\n";
		}
		my $class="gri_odd";
		my $actions="&nbsp;";
		my $l=link_to_str("stdlink","View","docrepos_view_document","section=" . _encode_base64($section) . ",document_sha1=$cr->{content_sha1},lname=$cr->{local_name}");
		$actions.="$l&nbsp;&nbsp;";
		if($allow_delete) {
			$l=link_to_str("stdlink","Delete","docrepos_delete_document_confirm","section=" . _encode_base64($section) . ",document_sha1=$cr->{content_sha1},lname=$cr->{local_name},lp=" . string_to_hex("$url_prefix$::ENV{REQUEST_URI}"));
			$actions.="$l&nbsp;&nbsp;";
			$l=link_to_str("stdlink","Replace","form_docrepos_replace_document_upload","section=" . _encode_base64($section) . ",document_sha1=$cr->{content_sha1},lname=$cr->{local_name},lp=" . string_to_hex("$url_prefix$::ENV{REQUEST_URI}"));
			$actions.="$l&nbsp;&nbsp;";
		}
		if(length($history_link)) {
			$actions.="$history_link&nbsp;&nbsp;";
		}
		
		$class="gri_even" if $row%2==0;
		$s.="<tr id=$class><td class=gri_cell>$cdoc_num</td>\n";
		$s.="<td class=gri_cell>$_name</td>\n";
		$s.="<td class=gri_cell>$uploaded</td>\n";
		$s.="<td class=gri_cell>$size</td>\n";
		$s.="<td class=gri_cell>$type</td>\n";
		$s.="</tr>\n";
		$s.="<tr id=$class><td class=gri_cell>&nbsp;</td>\n";
		my $xx=decode_base64($cr->{description});
		$xx =~ s/\n/<br>/g;
		$s.="<td class=gri_cell colspan=2>$xx</td>\n";
		$s.="<td class=gri_cell colspan=2>$actions</td>\n";
		$s.="</tr>\n";
		$row++;
		$cdoc_num++;
	}
	$s.="</table>\n";
	if($back_link) {
		my $pp=$page-1;
		$l=link_to_str("stdlink","Previous","docrepos_show_section_docs","section=" . _encode_base64($section) . ",page=$pp,order=$order");
		$s.="$l&nbsp; &nbsp; &nbsp;";
	}
	if($forward_link) {
		my $pp=$page+1;
		$l=link_to_str("stdlink","Next","docrepos_show_section_docs","section=" . _encode_base64($section) . ",page=$pp,order=$order");
		$s.="$l";
	}
	$s.="<P>";
	# Firstly add an upload link if applicable...
	if($allow_delete) {
		my $lp="docrepos_show_section_docs?section=" . _encode_base64($section) . ",page=$page,order=$order";
		$lp=unpack("h*",$lp);
		$l=link_to_str("stdlink","Upload","form_docrepos_upload_file","lp=$lp,file_section=" . _encode_base64($section));
		$s.="$l&nbsp; &nbsp; &nbsp;";
	}

	# $l=link_to_str("stdlink","Document Repository","docrepos_sections_chooser","");
	# $s.="$l&nbsp; &nbsp; &nbsp;";
	# $l=link_to_str("stdlink","Reports Portal","","");
	# $s.="$l&nbsp; &nbsp; &nbsp;";
	# $l=link_to_str("stdlink","Search","form_docrepos_search","");
	# $s.="$l\n";

	return $s;
}

1;

